<?php
require_once ('library/Controller/IController.php');
require_once ('library/Controller/BaseController.php');

/**
 *
 * @author Nguyen
 *        
 */
class UserController extends BaseController implements IController {
	/**
	 * 
	 * @var User
	 */
	protected $_model;
	public function loginAction(){
		$this->view()->setHasLayout('');
		$account = $this->getParram('account','');
		$password  = $this->getParram('password','');
		if($account == '' || $password = '')
			return ;
		$loginInfo =  $this->_model->login($account, $password);
		if($loginInfo != null){
			Auth::setInstance($loginInfo);
			$this->_redirect('',true);
		}
	}
	public function logoutAction(){
		if(Auth::getInstance() != null)
			Auth::clearInstance();
		$this->_redirect('',true);
	}
	/* (non-PHPdoc)
	 * @see BaseController::editAction()
	 */
	public function editAction() {
		// TODO Auto-generated method stub
		$c = new Company();
		$this->view ()->relativeData = $c->getallNameCompany ();
		$userpow = new Userpower();
		$this->view ()->relativeData1 = $userpow->getallNameUserPower();
		if ($this->isPost ()) {
			$entity = $this->getEntityData ();
			switch ($this->_actionType) {
				case 'insert' :
					echo 'insert';
					$this->view ()->objectData = $this->_model->insert ( $entity );
					break;
				case 'update' :
					echo 'update';
					$this->view ()->objectData = $this->_model->update ( $entity );
					break;
			}
			$this->_redirect ( '?_module=User&_action=getall' );
		} else {
			$id = $this->_model->getId ();
			$entity = array ();
			foreach ( $id as $value ) {
				$param = '_' . $this->_context . $value;
				if ($this->getParram ( $param, '' ) != '')
					$entity [$value] = $this->getParram ( $param, '' );
			}
			if (count ( $entity ) > 0) {
				$this->view ()->objectData = $this->_model->getbyid ( $entity );
				$this->view ()->title = Resource::getResource('edit') ." ".Resource::getResource($this->_context) ;
			}
			if (count ( $entity ) == 0) {
				$this->view ()->title = Resource::getResource('insert') ." ".Resource::getResource($this->_context) ;
			}
		}
	}
	
	
	
}

?>